// Copyright 2024 Tvux
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package _026

// removeDuplicates
// 双重循环，内层循环找到不同的那个元素然后移到前面，软删除。
func removeDuplicates(nums []int) int {
	if len(nums) == 0 {
		return 0
	}
	i, j := 0, 0
	for i < len(nums) {
		for nums[i] == nums[j] {
			j++
			if j == len(nums) {
				return i + 1
			}
		}
		nums[i+1] = nums[j]
		i++
	}
	return i
}

// removeDuplicates
// 使用map来充当一个哈希表，记录不重复的元素，然后再遍历这个哈希表返回。
//func removeDuplicates(nums []int) int {
//	s := make(map[int]struct{})
//	for i := 0; i < len(nums); i++ {
//		if _, ok := s[nums[i]]; !ok {
//			s[nums[i]] = struct{}{}
//		}
//	}
//	res := make([]int, 0, len(s))
//	for k := range s {
//		res = append(res, k)
//	}
//	sort.Ints(res)
//	copy(nums, res)
//	return len(s)
//}
